package com.pms.service.impl;

import com.aliyun.oss.model.SelectObjectException;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.pms.constant.MessageConstant;
import com.pms.dto.CommunityPageQueryDTO;
import com.pms.dto.CommunitySaveDTO;
import com.pms.entity.Community;
import com.pms.entity.Owner;
import com.pms.exception.DeletionNotAllowedException;
import com.pms.exception.InsertNotAllowException;
import com.pms.exception.NotFoundException;
import com.pms.mapper.BuildingMapper;
import com.pms.mapper.CommunityMapper;
import com.pms.mapper.OwnerMapper;
import com.pms.result.PageResult;
import com.pms.service.CommunityService;
import com.pms.vo.CommunityPageQueryVO;
import com.pms.vo.CommunitySelectVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.List;

@Service
public class CommunityServiceImpl implements CommunityService {

    @Autowired
    private CommunityMapper communityMapper;
    @Autowired
    private OwnerMapper ownerMapper;
    @Autowired
    private BuildingMapper buildingMapper;

    /**
     * 新增小区
     * @param communitySaveDTO
     */
    public void saveCommunity(CommunitySaveDTO communitySaveDTO) {
        Community communityMapperByName= communityMapper.getByName(communitySaveDTO.getName());
        if(communityMapperByName==null){
            Community community = new Community();
            BeanUtils.copyProperties(communitySaveDTO,community);
            communityMapper.insert(community);
        }else{
            throw new InsertNotAllowException(MessageConstant.RECORD_EXIST);
        }
    }

    /**
     * 小区分页查询
     * @param communityPageQueryDTO
     * @return
     */
    public PageResult pageQuery(CommunityPageQueryDTO communityPageQueryDTO) {
        PageHelper.startPage(communityPageQueryDTO.getPage(), communityPageQueryDTO.getPageSize());
        Page<CommunityPageQueryVO> page=communityMapper.pageQuery(communityPageQueryDTO);
        return new PageResult(page.getTotal(),page.getResult());
    }

    /**
     * 根据id批量删除
     * @param ids
     */
    @Transactional
    public void delete(List<Integer> ids) {
        for (Integer id : ids) {
            communityMapper.delete(id);
            // 同时删除对应楼栋信息
            buildingMapper.deleteByCommunity(id);
        }
    }

    /**
     * 修改信息
     * @param community
     */
    public void update(Community community) {
        communityMapper.update(community);
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    public Community getById(Integer id) {
        Community community=communityMapper.getById(id);

        if (community==null){
            throw new NotFoundException(MessageConstant.ID_NOT_EXIST);
        }
        return community;
    }

    /**
     * 小区选择列表
     * @return
     */
    public List<CommunitySelectVO> list() {
        List<CommunitySelectVO> voList=communityMapper.list();
        return voList;
    }
}
